﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ServiceBus.Contract;
using ServiceBus.EndPoint;

namespace Test
{
    public partial class FmMain : Form
    {
        public FmMain()
        {
            InitializeComponent();
            FunctionServiceConnector.AddKnownType(typeof(Inventory));
            FunctionServiceConnector.AddKnownType(typeof(Inventory[]));
        }
        ServiceBus.EndPoint.SAOServiceStructre<IStandardService> MyService  = null;
        delegate void MessageInvoker(object sender, ServiceBus.Contract.Message e);
        void messager_MessageDelivered(object sender, ServiceBus.Contract.Message e)
        {
            if (this.InvokeRequired)
            {
                MessageInvoker invoker = new MessageInvoker(messager_MessageDelivered);
                this.Invoke(invoker, sender, e);
            }
            else
            {
                this.LoadInventoryList();
                int productid = Convert.ToInt32(e.PrimaryID);
                Inventory inventory = this.GetService().Invoke("GetInventory", productid) as Inventory;
                switch (e.FunctionCode.ToUpper().Trim())
                {
                    case "NEWINVENTORY":
                        this.txtInfo.AppendText(e.Sender + " created new product " +
                            inventory.ProductName + " with inventory: " + inventory.CurrentInventory.ToString() + "\r\n");
                        break;
                    case "OUTOFSTOCK":
                        this.txtInfo.AppendText(e.Sender + " change " + inventory.ProductName + " inventory fail: out of stock \r\n");
                        break;
                    case "INVENTORYCHANGE":
                        this.txtInfo.AppendText(e.Sender + " changed " + inventory.ProductName + " inventory to " +
                            inventory.CurrentInventory.ToString() + "\r\n");
                        break;
                    default:
                        break;
                }
            }
        }

        Color StatusColor = Color.Green;
        void messager_ServerDisconnectedEvent(object sender, EventArgs e)
        {
            this.StatusColor = Color.Gray;
            this.pnlTop.Invalidate(false);
        }
        private IStandardService GetService()
        {
            return this.MyService.GetFunctionService<IStandardService>();
        }
        private void LoadInventoryList()
        {
            IStandardService service = this.GetService();
            this.inventoryBindingSource.DataSource = null;
            this.inventoryBindingSource.DataSource = 
                ((Inventory[])service.Invoke("GetInventoryList", null)).ToList();
        }
        void Instance_HeartBeatFromServer(object sender, EventArgs e)
        {
            if (this.StatusColor == Color.Gray) this.StatusColor = Color.Green;
            else this.StatusColor = Color.Gray;
            this.pnlTop.Invalidate(false);
        }

        private void FmMain_Load(object sender, EventArgs e)
        {
            //this.LoadInventoryList();
        }

        private void pnlTop_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            Rectangle rec = new Rectangle(2, 2, 12, 12);
            e.Graphics.FillEllipse(new SolidBrush(this.StatusColor), rec);
            e.Graphics.DrawEllipse(new Pen(this.StatusColor), rec);
        }

        private void dgInventory_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            Inventory inventory = this.dgInventory.Rows[e.RowIndex].DataBoundItem as Inventory;
            if (inventory == null) return;
            if (inventory.ProductID == 0 && inventory.ProductName.Trim()!="" &&
                inventory.CurrentInventory!=0)
            {
                inventory.ProductID = (int)this.GetService().Invoke("CreateNewProduct", inventory, this.txtUser.Text);
            }

            if(inventory.ProductID>0)
            {
                Inventory inv = this.GetService().Invoke("GetInventory",inventory.ProductID) as Inventory;
                this.GetService().Invoke("ChangeInventory", this.txtUser.Text, inventory.ProductID, 
                    inventory.CurrentInventory-inv.CurrentInventory);
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            this.MyService = new SAOServiceStructre<IStandardService>("myservice",this.txtServer.Text);
            MessageDistributor messager = this.MyService.MessageService;
            messager.HeartBeatFromServer += new EventHandler(Instance_HeartBeatFromServer);
            messager.ServerDisconnectedEvent += new EventHandler(messager_ServerDisconnectedEvent);
            messager.MessageDelivered += new EventHandler<ServiceBus.Contract.Message>(messager_MessageDelivered);
            this.LoadInventoryList();
        }
    }
}
